//
//  RGXTabBarController.m
//  YuCaiTong
//
//  Created by rgx on 16/8/21.
//  Copyright © 2016年 rgx. All rights reserved.
//

#import "RGXTabBarController.h"
#import "RGXOneController.h"
#import "RGXTwoController.h"
#import "RGXThreeController.h"
#import "RGXFourController.h"
#import "RGXNavigationController.h"
@interface RGXTabBarController (){
    
    RGXOneController *oneVC;
    RGXTwoController *twoVC;
    RGXThreeController *threeVC;
    RGXFourController *fourVC;
}

@end

@implementation RGXTabBarController


static RGXTabBarController *stBar = nil;

+(RGXTabBarController*)shareBarController
{
    return  stBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    //设置UITabbar 背景色
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.tabBar insertSubview:bgView atIndex:0];
    self.tabBar.opaque = YES;
    
    [self setUpAllChildViewController];
    
    
}



/**
 *  添加所有子控制器
 */
- (void)setUpAllChildViewController{
    // 1.添加第一个控制器
    oneVC = [[RGXOneController alloc]initWithNibName:@"RGXOneController" bundle:nil];
    [self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@"one_gray"] selectedImage:[UIImage imageNamed:@"one_red"] title:@"首页"];
    
    // 2.添加第2个控制器
    twoVC = [[RGXTwoController alloc]initWithNibName:@"RGXTwoController" bundle:nil];
    
    [self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@"two_gray"] selectedImage:[UIImage imageNamed:@"two_red"] title:@"开奖公告"];
    
    // 3.添加第3个控制器
  
    threeVC = [[RGXThreeController alloc]initWithNibName:@"RGXThreeController" bundle:nil];
    [self setUpOneChildViewController:threeVC image:[UIImage imageNamed:@"three_gray"]  selectedImage:[UIImage imageNamed:@"three_red"] title:@"我的彩票"];
    
    // 4.添加第4个控制器
    fourVC = [[RGXFourController alloc]initWithNibName:@"RGXFourController" bundle:nil];
    
    [self setUpOneChildViewController:fourVC image:[UIImage imageNamed:@"four_gray"] selectedImage:[UIImage imageNamed:@"four_red"] title:@"个人中心"];
    
    UIColor *txtColor=[UIColor colorWithRed:231.0/255.0 green:66/255.0 blue:66/255.0 alpha:1];
    
    NSMutableDictionary *atts=[NSMutableDictionary dictionary];
    atts[NSFontAttributeName]=[UIFont systemFontOfSize:12];
    atts[NSForegroundColorAttributeName]=[UIColor grayColor];
    
    NSMutableDictionary *selectedAtts=[NSMutableDictionary dictionary];
    selectedAtts[NSFontAttributeName]=atts[NSFontAttributeName];
    selectedAtts[NSForegroundColorAttributeName]= txtColor;
    // 在这里 只要更改，所有的文字都改
    UITabBarItem *item=[UITabBarItem appearance];
    [item setTitleTextAttributes:atts forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAtts forState:UIControlStateSelected];
 
}


/**
 *  添加一个子控制器的方法
 */
- (void)setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title{
    
    RGXNavigationController *navC = [[RGXNavigationController alloc]initWithRootViewController:viewController];
    navC.title = title;

    navC.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navC.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    //导航栏背景颜色
    navC.navigationBar.barTintColor = ColorWithRGB(231, 66, 66);
    navC.navigationBar.translucent = NO;
    viewController.navigationItem.title = title;


    //导航栏标题颜色
    [viewController.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //返回键字体颜色
    viewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [self addChildViewController:navC];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    //    如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
        result=nav.childViewControllers.lastObject;
        
    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }else{
        result = nextResponder;
    }
    
    return result;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
